﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication2.GeocodeService;
using WpfApplication2.ImageryService;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static String key = "Akkl5AO_xE0nt7bv4pklYDpB1ShITJrNmSUjumbMyoOPBggk0T_n5U9r0cn_ucaE";
        
   
        public MainWindow()
        {
            InitializeComponent();
            
        }
        public GeocodeResult[] GeocodeAddress(string address)
        {
             GeocodeRequest req = new GeocodeRequest();

            req.Credentials = new GeocodeService.Credentials();
            req.Credentials.ApplicationId = key;

            req.Query = address;

            ConfidenceFilter[] filters = new ConfidenceFilter[1];
            filters[0] = new ConfidenceFilter();
            filters[0].MinimumConfidence = GeocodeService.Confidence.High;

            GeocodeOptions opts = new GeocodeOptions();
            opts.Filters = filters;
            req.Options = opts;

            GeocodeServiceClient client = new GeocodeServiceClient();
            GeocodeResponse resp = client.Geocode(req);


            return resp.Results;
        }

        private string GetImagery( double lon, double lat )
        {
            MapUriRequest req = new MapUriRequest();

            req.Credentials = new ImageryService.Credentials();
            req.Credentials.ApplicationId = key;

            req.Center = new ImageryService.Location();
            req.Center.Longitude = lon;
            req.Center.Latitude = lat;

            MapUriOptions opts = new MapUriOptions();
            opts.Style = MapStyle.AerialWithLabels;

            opts.ImageSize = new ImageryService.SizeOfint();
            opts.ImageSize.Height = 200;
            opts.ImageSize.Width = 300;

            req.Options = opts;

            ImageryServiceClient client = new ImageryServiceClient();
            MapUriResponse resp = client.GetMapUri(req);

            return resp.Uri;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if ("".Equals(textInput.Text))
            {
                resultsLabel.Content = "No results found";
                resultsLabel.Visibility = Visibility.Visible;
                resultImage.Visibility = Visibility.Hidden;
                return;
            }
            GeocodeResult[] results = GeocodeAddress(textInput.Text);

            if (results.Length == 0)
            {
                resultsLabel.Content = "No results found";
                resultsLabel.Visibility = Visibility.Visible;
                resultImage.Visibility = Visibility.Hidden;
            }
            else
            {
                String imageURI = GetImagery(results[0].Locations[0].Longitude, results[0].Locations[0].Latitude);
                resultImage.Source = new BitmapImage(new Uri(imageURI));
                resultsLabel.Visibility = Visibility.Hidden;
                resultImage.Visibility = Visibility.Visible;
            }
        }
    }
}
